package com.wanxi.dangdang.controller.back.order;

import com.github.pagehelper.PageInfo;
import com.wanxi.dangdang.common.CommonResult;
import com.wanxi.dangdang.common.OrderStateCode;
import com.wanxi.dangdang.model.pojo.Order;
import com.wanxi.dangdang.model.vo.OrderPo;
import com.wanxi.dangdang.model.vo.OrderStateVo;
import com.wanxi.dangdang.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Author: 寇文宝
 * @Description: 后台订单管理，管理员处理订单业务时使用
 * @Date: Created in 2021/8/30
 * @Version: 1.0
 */

@Api(tags = "后台订单管理，均需要登录")
@RestController
@RequestMapping("/back/a/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("订单管理列表分页查询")
    @GetMapping("/findPage")
    public CommonResult<PageInfo<List<OrderPo>>> findPage(Integer pageNum, Integer pageSize) {
        PageInfo orderList = orderService.findPage(pageNum, pageSize);
        return CommonResult.success(orderList);
    }

    @ApiOperation("通过订单id查询订单详情信息")
    @GetMapping("/findById")
    public CommonResult<OrderPo> findById(Long id) {
        OrderPo orderById = orderService.findById(id);
        return CommonResult.success(orderById);
    }

    @ApiOperation("修改订单状态")
    @PostMapping("/updateOrderState")
    @ApiParam(name = "orderStateVo", value = "订单状态vo类，传递订单id和订单状态")
    public CommonResult updateOrderState(@RequestBody OrderStateVo orderStateVo) {
        String updateOrderState = orderService.updateOrderState(orderStateVo);
        return CommonResult.success(updateOrderState);
    }

    @ApiOperation("管理员确认是否退款")
    @PostMapping("/confirmRefund")
    @ApiParam(name = "orderStateVo", value = "订单状态vo类，传递订单id和订单状态")
    public CommonResult confirmRefund(@RequestBody OrderStateVo orderStateVo, HttpSession session) {
        Long id = (Long) session.getAttribute("id");
        orderStateVo.setAdminId(id);
        String confirmRefund = orderService.confirmRefund(orderStateVo);
        return CommonResult.success(confirmRefund);
    }

    @ApiOperation("通过订单状态查询")
    @GetMapping("/findByOrderState")
    public CommonResult<PageInfo<List<Order>>> findByOrderState(Integer orderState,Integer pageNum, Integer pageSize) {
        PageInfo byOrderState = orderService.findByOrderState(orderState, pageNum, pageSize);
        return CommonResult.success(byOrderState);
    }

    @ApiOperation("通过订单编号精准查询")
    @GetMapping("/findByOrderNumber")
    public CommonResult<Order> findByOrderNumber(String orderNumber) {
        Order byOrderNumber = orderService.findByOrderNumber(orderNumber);
        return CommonResult.success(byOrderNumber);
    }

    @ApiOperation("认领订单的同时修改订单状态")
    @PostMapping("/updateOrderStatusAndAdminId")
    @ApiParam(name = "orderStateVo", value = "订单状态vo类，传递订单id和负责人id")
    public CommonResult updateStatusAndAdminId(@RequestBody OrderStateVo orderStateVo, HttpSession session) {
        Long id = (Long) session.getAttribute("id");
        orderStateVo.setAdminId(id);
        if (orderStateVo.getAdminId() != 0 && orderStateVo.getOrderState().equals(OrderStateCode.PAID.getCode())) {
            orderService.updateStatusAndAdminId(orderStateVo);
            return CommonResult.success("认领成功");
        }
        return CommonResult.failed("认领失败");
    }

    @ApiOperation("管理员查询自己的已认领订单")
    @GetMapping("/findByAdminId")
    public CommonResult<PageInfo<List<Order>>> findByAdminId(HttpSession session,Integer pageNum,Integer pageSize) {
        Long adminId = (Long) session.getAttribute("id");
        PageInfo byAdminId = orderService.findByAdminId(adminId,pageNum,pageSize);
        return CommonResult.success(byAdminId);
    }

    @ApiOperation("查询当前日期一周内每日的所有订单数")
    @GetMapping("/findAllByDate")
    public CommonResult<PageInfo<List<Order>>> findAllByDate(){
        Map<String, Integer> allOrdersByDate = orderService.findAllOrdersByDate();
        return CommonResult.success(allOrdersByDate);
    }


    @ApiOperation("查询当前日期一周内每日的订单总销售价")
    @GetMapping("/findPriceTotalByDate")
    public CommonResult<PageInfo<List<Order>>> findPriceTotalByDate() {
        Map<String, BigDecimal> priceTotalByDate = orderService.findPriceTotalByDate();
        return CommonResult.success(priceTotalByDate);
    }


}
